package com.example.rqweb;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class info extends AppCompatActivity {
Button b1,b2;
    private Handler hd=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {


            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }

    public void getinfo()
    {
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                new Thread(new Runnable() {
                    @Override
                    public void run()
                    {
                        try {
                            URL url=new URL("http://10.0.2.2:8080/");


                            HttpURLConnection coon= (HttpURLConnection) url.openConnection();
                            coon.setRequestMethod("get");
                            InputStream input=coon.getInputStream();
                            byte[]s=new byte[200];
                            String info= String.valueOf(input.read(s));

                            Message ms=new Message();
                            Object gf = ms.obj = info;

                            hd.sendMessage(ms);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }
                }).start();
            }
        });



    }
    public  void sendinfo()
    {

            b2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    new Thread(new Runnable() {
                        @Override
                        public void run()
                        {
                            try {
                                URL url=new URL("http://10.0.2.2:8080/");

                                HttpURLConnection coon= (HttpURLConnection) url.openConnection();
                                coon.setRequestMethod("post");
                                coon.setDoInput(true);
                              DataOutputStream out=( DataOutputStream)coon.getOutputStream();
                                byte[]s=new byte[200];
                                out.writeBytes(String.valueOf(s));
                                 out.flush();


                            } catch (IOException e) {
                                e.printStackTrace();
                            }


                        }
                    }).start();
                }
            });


    }
}
